package com.winit.openapi.isp.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.oms.spi.isp.ISPOrderPickUpMessageService;
import com.winit.oms.spi.isp.command.ISPOrderPickUpMessageCommand;
import com.winit.oms.spi.isp.command.ISPOrderPickUpMessageSearchCommand;
import com.winit.oms.spi.isp.vo.ISPOrderPickUpMessage;
import com.winit.oms.spi.isp.vo.ISPOrderPickUpPackage;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.util.ValidateUtil;
import com.winit.tp.spi.trace.command.GetOriginalTracesCommand;
import com.winit.tp.spi.trace.vo.OriginalTrace;

/**
 * ISP揽收单Controller
 * <Change to the actual description of this class>
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * jian.ma 	1.0  		2017年5月24日 	Created
 *
 * </pre>
 * @since 1.
                       .::::.
                     .::::::::.
                    :::::::::::
                 ..:::::::::::'
              '::::::::::::'
                .::::::::::
           '::::::::::::::..
                ..::::::::::::::.
              ``::::::::::::::::::    妹子保佑，永无bug
               ::::``:::::::::::'      .:::.
              ::::'   ':::::'       .::::::::.
            .::::'      ::::     .:::::::'::::.
           .:::'       :::::  .:::::::::' ':::::.
          .::'        :::::.:::::::::'      ':::::.
         .::'         ::::::::::::::'         ``::::.
     ...:::           ::::::::::::'              ``::.
    ```` ':.          ':::::::::'                  ::::..
                       '.:::::'                    ':'````..
 */
@RequestMapping(value = "/isp/pickup")
@Controller
public class ISPOrderPickUpController extends BaseController {
    @Resource
    private ISPOrderPickUpMessageService iSPOrderPickUpMessageService;
    
    
    
    @RequestMapping(value = "/importValid")
    public String importValid(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "expressNos");
            String bPartnerId = CommandContext.getContext().getUserId();
            List<Map<String,Object>> list = iSPOrderPickUpMessageService.importValid(json.getString("expressNos"),bPartnerId,json.getString("pickupId"));
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", list);
            responseMsg.setData(data);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

    
    
    
    
    @RequestMapping(value = "/confirm")
    public String confirm(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            validConfirm(json);
            ISPOrderPickUpMessage iSPOrderPickUpMessage = this.buildConfirm(json);
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            iSPOrderPickUpMessageService.confirm(command);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        
        return SUCCESS;
    }
    

    private ISPOrderPickUpMessage buildConfirm(JSONObject json) {
        ISPOrderPickUpMessage iSPOrderPickUpMessage = new ISPOrderPickUpMessage();
        iSPOrderPickUpMessage.setPickupId(json.getString("pickupId"));
        iSPOrderPickUpMessage.setReceiveTime(json.getDate("receiveTime"));
        if(json.containsKey("cartonNumber")){
            iSPOrderPickUpMessage.setCfmCartonNumber(json.getInteger("cartonNumber"));
        }
        if(json.containsKey("packageNumber")){
            iSPOrderPickUpMessage.setCfmPackageNumber(json.getInteger("packageNumber"));
        }
        if(json.containsKey("totalweight")){
            iSPOrderPickUpMessage.setCfmTotalWeight(json.getBigDecimal("totalweight"));
        }
        List<ISPOrderPickUpPackage> list = new ArrayList<ISPOrderPickUpPackage>();
        JSONArray packageList = json.getJSONArray("orderList");
        if(packageList!=null){
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ISPOrderPickUpPackage iSPOrderPickUpPackage = new ISPOrderPickUpPackage();
                iSPOrderPickUpPackage.setOrderNo(pack.getString("orderNO"));
                iSPOrderPickUpPackage.setExpressNo(pack.getString("trackNO"));
                iSPOrderPickUpPackage.setUpdatedBy(CommandContext.getContext().getUsername());
                iSPOrderPickUpPackage.setUpdated(new Date());
                list.add(iSPOrderPickUpPackage);
            }
            iSPOrderPickUpMessage.setiSPOrderPickUpPackage(list);
        }
        return iSPOrderPickUpMessage;
    }


    private void validConfirm(JSONObject json) {
        ValidateUtil.validNotNull(json, "pickupId");
        ValidateUtil.validMaxLength(json, "pickupId", 50);
        ValidateUtil.validNotNull(json, "receiveTime");
        ValidateUtil.validDateString(json, "receiveTime", DateUtil.FULL_DATE_STRING);
        ValidateUtil.validMaxLength(json, "cartonNumber", 5);
        ValidateUtil.validInteger(json, "cartonNumber", false);
        ValidateUtil.validMaxLength(json, "packageNumber", 5);
        ValidateUtil.validInteger(json, "packageNumber", false);
        ValidateUtil.validBigDecimal(json, "totalweight", false);
        JSONArray packageList = json.getJSONArray("orderList");
        if(packageList!=null){
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ValidateUtil.validNotNull(pack, "orderNO");
                ValidateUtil.validMaxLength(pack, "orderNO", 50);
                ValidateUtil.validNotNull(pack, "trackNO");
                ValidateUtil.validMaxLength(pack, "trackNO", 50);
            }
        }
        
    }





    @RequestMapping(value = "/orderList")
    public String orderList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        ISPOrderPickUpMessageSearchCommand command = new ISPOrderPickUpMessageSearchCommand();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            validSearch(json);
            command = buildSearchCommand(json);
            Searchable<?> searchable = buildSearchPageParams(json);
            command.setSearchable(searchable);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());
        

        // 调用接口分页查询订单
        Page<ISPOrderPickUpMessage> pagelist = iSPOrderPickUpMessageService.getISPOrderPickUpMessageByCondition(command);
        if (pagelist != null) {
            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("pageNo", pagelist.getPageable().getPageNumber());
            pageMap.put("pageSize", pagelist.getPageable().getPageSize());
            pageMap.put("totalCount", pagelist.getTotalElements());
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", pagelist.getContent());
            data.put("pageParams", pageMap);
            responseMsg.setData(data);
        }
        
        return SUCCESS;
    }

    private void validSearch(JSONObject json) {
        
        ValidateUtil.validMaxLength(json, "pickupId", 50);
        
        ValidateUtil.validMaxLength(json, "pickupType", 10);
        
        ValidateUtil.validDateString(json, "startPickupPlanDate", DateUtil.SIMPLE_DATE_STRING);
        ValidateUtil.validDateString(json, "endPickupPlanDate", DateUtil.SIMPLE_DATE_STRING);
        ValidateUtil.validDateBefore(json, "startPickupPlanDate", "endPickupPlanDate");

        // pageParams
        if (json.containsKey("pageParams")) {
            Object obj = json.get("pageParams");
            if (obj instanceof JSONObject) {
                JSONObject pageParams = (JSONObject) obj;
                // pageNo
                ValidateUtil.validInteger(pageParams, "pageNo");

                // pageSize
                ValidateUtil.validInteger(pageParams, "pageSize");

                if (isExistValue(pageParams, "pageNo")) {
                    int pageNo = pageParams.getInteger("pageNo");
                    if (pageNo < 1) {
                        throw new ApiException(ErrorCode.FIELD_ERROR, "pageNo");
                    }
                }
                if (isExistValue(pageParams, "pageSize")) {
                    int pageSize = pageParams.getInteger("pageSize");
                    if (pageSize < 1) {
                        throw new ApiException(ErrorCode.FIELD_ERROR, "pageSize");
                    }
                }
            } else {
                throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "pageParams");
            }
        }
    }
    
    private ISPOrderPickUpMessageSearchCommand buildSearchCommand(JSONObject json) {
        ISPOrderPickUpMessageSearchCommand command = new ISPOrderPickUpMessageSearchCommand();
        if (json.containsKey("pickupId")) {
            command.setPickupId(StringUtils.trim(json.getString("pickupId")));
        }
        if (json.containsKey("pickupType")) {
            command.setPickupType(StringUtils.trim(json.getString("pickupType")));
        }
        if (json.containsKey("startPickupPlanDate")) {
            command.setStartPickupPlanDate(StringUtils.trim(json.getString("startPickupPlanDate")));
        }
        if (json.containsKey("endPickupPlanDate")) {
            command.setEndPickupPlanDate(StringUtils.trim(json.getString("endPickupPlanDate")));
        }
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        command.setSellerId(bpartner.getValue());
        return command;
    }
    
    private Searchable<?> buildSearchPageParams(JSONObject json) {
        int pageNo = ApiConstant.PAGE_NO_DEFAULT;
        int pageSize = ApiConstant.PAGE_SIZE_DEFAULT;
        // pageParams
        if (isExistValue(json, "pageParams")) {
            JSONObject pageParams = json.getJSONObject("pageParams");

            if (ValidateUtil.isIntNumber(pageParams.getString("pageNo"))) {
                pageNo = pageParams.getInteger("pageNo");
                if (pageNo <= 0) {
                    throw new ApiException(ErrorCode.FIELD_ERROR, "pageNo");
                }
            } else if (isExistValue(pageParams, "pageNo") && !ValidateUtil.isIntNumber(pageParams.getString("pageNo"))) {
                throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "pageNo");
            }

            if (ValidateUtil.isIntNumber(pageParams.getString("pageSize"))) {
                pageSize = pageParams.getInteger("pageSize");
                if (pageSize <= 0) {
                    throw new ApiException(ErrorCode.FIELD_ERROR, "pageSize");
                }
            } else if (isExistValue(pageParams, "pageSize")
                       && !ValidateUtil.isIntNumber(pageParams.getString("pageSize"))) {
                throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "pageSize");
            }

        }
        Searchable<?> search = Searchable.newSearchable();
        search.setPage(pageNo, pageSize);
        search.addSort(new Sort(Direction.ASC, "CREATED"));
        return search;
    }
    
    
    
    @RequestMapping(value = "/add")
    public String add(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            validAdd(json);
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            ISPOrderPickUpMessage iSPOrderPickUpMessage = buildAdd(json);
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            iSPOrderPickUpMessageService.add(command);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    @RequestMapping(value = "/update")
    public String update(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "pickupId");
            ValidateUtil.validMaxLength(json, "pickupId", 50);
            ValidateUtil.validMaxLength(json, "cartonNumber", 5);
            ValidateUtil.validMaxLength(json, "packageNumber", 5);
            ValidateUtil.validNotNull(json, "totalWeight");
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            ISPOrderPickUpMessage iSPOrderPickUpMessage = new ISPOrderPickUpMessage();
            iSPOrderPickUpMessage.setPickupId(json.getString("pickupId"));
            Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
            iSPOrderPickUpMessage.setSellerId(bpartner.getValue());
            iSPOrderPickUpMessage.setCartonNumber(json.getIntValue("cartonNumber"));
            iSPOrderPickUpMessage.setPackageNumber(json.getIntValue("packageNumber"));
            iSPOrderPickUpMessage.setTotalWeight(json.getBigDecimal("totalWeight"));
            List<ISPOrderPickUpPackage> list = new ArrayList<ISPOrderPickUpPackage>();
            JSONArray packageList = json.getJSONArray("iSPOrderPickUpPackage");
            if(packageList!=null){
                for (Object o : packageList) {
                    JSONObject pack = (JSONObject) o;
                    ISPOrderPickUpPackage iSPOrderPickUpPackage = new ISPOrderPickUpPackage();
                    iSPOrderPickUpPackage.setOrderNo(pack.getString("orderNo"));
                    iSPOrderPickUpPackage.setExpressNo(pack.getString("expressNo"));
                    iSPOrderPickUpPackage.setCreatedBy(CommandContext.getContext().getUsername());
                    iSPOrderPickUpPackage.setCreated(new Date());
                    iSPOrderPickUpPackage.setUpdatedBy(CommandContext.getContext().getUsername());
                    iSPOrderPickUpPackage.setUpdated(new Date());
                    iSPOrderPickUpPackage.setOrderDate(pack.getDate("orderDate"));
                    list.add(iSPOrderPickUpPackage);
                }
                iSPOrderPickUpMessage.setiSPOrderPickUpPackage(list);
            }
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            iSPOrderPickUpMessageService.updateWeightToYw(command);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    @RequestMapping(value = "/cancel")
    public String cancel(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "pickupId");
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            ISPOrderPickUpMessage iSPOrderPickUpMessage = new ISPOrderPickUpMessage();
            iSPOrderPickUpMessage.setPickupId(json.getString("pickupId"));
            Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
            iSPOrderPickUpMessage.setSellerId(bpartner.getValue());
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            iSPOrderPickUpMessageService.cancel(command);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    @RequestMapping(value = "/printPickUpMessage")
    public String printPickUpMessage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "pickupId");
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            ISPOrderPickUpMessage iSPOrderPickUpMessage = new ISPOrderPickUpMessage();
            iSPOrderPickUpMessage.setPickupId(json.getString("pickupId"));
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            String fileCode = iSPOrderPickUpMessageService.printPickUpMessage(command);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("fileCode", fileCode);
            responseMsg.setData(data);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

    
    @RequestMapping(value = "/printZTPickUpMessage")
    public String printZTPickUpMessage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "pickupId");
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            ISPOrderPickUpMessage iSPOrderPickUpMessage =new ISPOrderPickUpMessage();
            iSPOrderPickUpMessage.setPickupId(json.getString("pickupId"));
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            String fileCode = iSPOrderPickUpMessageService.printZTPickUpMessage(command);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("fileCode", fileCode);
            responseMsg.setData(data);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    @RequestMapping(value = "/printZTPickUpMessage2")
    public String printZTPickUpMessage2(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "pickupId");
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            ISPOrderPickUpMessage iSPOrderPickUpMessage =new ISPOrderPickUpMessage();
            iSPOrderPickUpMessage.setPickupId(json.getString("pickupId"));
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            String fileCode = iSPOrderPickUpMessageService.printZTPickUpMessage2(command);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("fileCode", fileCode);
            responseMsg.setData(data);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    private ISPOrderPickUpMessage buildAdd(JSONObject json) {
        ISPOrderPickUpMessage iSPOrderPickUpMessage = new ISPOrderPickUpMessage();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        iSPOrderPickUpMessage.setSellerId(bpartner.getValue());
        iSPOrderPickUpMessage.setSellerName(json.getString("sellerName"));
        iSPOrderPickUpMessage.setTelphone(json.getString("telphone"));
        iSPOrderPickUpMessage.setCountry(json.getString("country"));
        iSPOrderPickUpMessage.setProvince(json.getString("province"));
        iSPOrderPickUpMessage.setCity(json.getString("city"));
        if(json.getString("district")==null){
            iSPOrderPickUpMessage.setDistrict("-");
        }else{
            iSPOrderPickUpMessage.setDistrict(json.getString("district"));
        }
        iSPOrderPickUpMessage.setAddressCn(json.getString("addressCn"));
        iSPOrderPickUpMessage.setAddressCode(json.getString("addressCode"));
        iSPOrderPickUpMessage.setPickupType(json.getString("pickupType"));
        iSPOrderPickUpMessage.setWarehouseCode(json.getString("warehouseCode"));
        iSPOrderPickUpMessage.setWarehouseName(json.getString("warehouseName"));
        iSPOrderPickUpMessage.setExpressCompany(json.getString("expressCompany"));
        iSPOrderPickUpMessage.setExpressNo(json.getString("expressNo"));
        iSPOrderPickUpMessage.setPickupPlanDate(json.getString("pickupPlanDate"));
        iSPOrderPickUpMessage.setPickupPlanStartHour(json.getString("pickupPlanStartHour"));
        iSPOrderPickUpMessage.setPickupPlanEndHour(json.getString("pickupPlanEndHour"));
        iSPOrderPickUpMessage.setPickupName(json.getString("pickupName"));
        iSPOrderPickUpMessage.setPickupTel(json.getString("pickupTel"));
        iSPOrderPickUpMessage.setCartonNumber(json.getIntValue("cartonNumber"));
        iSPOrderPickUpMessage.setPackageNumber(json.getIntValue("packageNumber"));
        iSPOrderPickUpMessage.setTotalWeight(json.getBigDecimal("totalWeight"));
        iSPOrderPickUpMessage.setVendorCode(json.getString("vendorCode"));
        iSPOrderPickUpMessage.setPickUpWarehouseCode(json.getString("pickUpWarehouseCode"));
        List<ISPOrderPickUpPackage> list = new ArrayList<ISPOrderPickUpPackage>();
        JSONArray packageList = json.getJSONArray("iSPOrderPickUpPackage");
        if(packageList!=null){
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ISPOrderPickUpPackage iSPOrderPickUpPackage = new ISPOrderPickUpPackage();
                iSPOrderPickUpPackage.setOrderNo(pack.getString("orderNo"));
                iSPOrderPickUpPackage.setExpressNo(pack.getString("expressNo"));
                iSPOrderPickUpPackage.setCreatedBy(CommandContext.getContext().getUsername());
                iSPOrderPickUpPackage.setCreated(new Date());
                iSPOrderPickUpPackage.setUpdatedBy(CommandContext.getContext().getUsername());
                iSPOrderPickUpPackage.setUpdated(new Date());
                iSPOrderPickUpPackage.setOrderDate(pack.getDate("orderDate"));
                list.add(iSPOrderPickUpPackage);
            }
            iSPOrderPickUpMessage.setiSPOrderPickUpPackage(list);
        }
        return iSPOrderPickUpMessage;
    }

    private void validAdd(JSONObject json) {
        ValidateUtil.validNotNull(json, "vendorCode");
        ValidateUtil.validMaxLength(json, "vendorCode", 50);
        ValidateUtil.validNotNull(json, "sellerName");
        ValidateUtil.validMaxLength(json, "sellerName", 50);
        ValidateUtil.validNotNull(json, "telphone");
        ValidateUtil.validMaxLength(json, "telphone", 50);
        ValidateUtil.validNotNull(json, "country");
        ValidateUtil.validMaxLength(json, "country", 50);
        ValidateUtil.validNotNull(json, "province");
        ValidateUtil.validMaxLength(json, "province", 50);
        ValidateUtil.validNotNull(json, "city");
        ValidateUtil.validMaxLength(json, "city", 50);
        ValidateUtil.validMaxLength(json, "district", 50);
        ValidateUtil.validNotNull(json, "addressCn");
        ValidateUtil.validMaxLength(json, "addressCn", 100);
        ValidateUtil.validNotNull(json, "addressCode");
        ValidateUtil.validMaxLength(json, "addressCode", 50);
        ValidateUtil.validNotNull(json, "pickupType");
        ValidateUtil.validMaxLength(json, "pickupType", 10);
        ValidateUtil.validNotNull(json, "warehouseCode");
        ValidateUtil.validMaxLength(json, "warehouseCode", 50);
        ValidateUtil.validMaxLength(json, "pickUpWarehouseCode", 50);
        ValidateUtil.validNotNull(json, "warehouseName");
        ValidateUtil.validMaxLength(json, "warehouseName", 50);
        ValidateUtil.validMaxLength(json, "expressCompany", 100);
        ValidateUtil.validMaxLength(json, "expressNo", 100);
        ValidateUtil.validNotNull(json, "pickupPlanDate");
        ValidateUtil.validMaxLength(json, "pickupPlanDate", 10);
        ValidateUtil.validNotNull(json, "pickupPlanStartHour");
        ValidateUtil.validMaxLength(json, "pickupPlanStartHour", 10);
        ValidateUtil.validNotNull(json, "pickupPlanEndHour");
        ValidateUtil.validMaxLength(json, "pickupPlanEndHour", 10);
        ValidateUtil.validMaxLength(json, "pickupName", 100);
        ValidateUtil.validMaxLength(json, "pickupTel", 100);
        ValidateUtil.validMaxLength(json, "cartonNumber", 5);
        ValidateUtil.validMaxLength(json, "packageNumber", 5);
        ValidateUtil.validNotNull(json, "totalWeight");
        ValidateUtil.validNotNull(json, "vendorCode");
        ValidateUtil.validMaxLength(json, "vendorCode", 10);
        positiveNumber(json, "totalWeight");
        JSONArray packageList = json.getJSONArray("iSPOrderPickUpPackage");
        if(packageList!=null){
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ValidateUtil.validNotNull(pack, "orderNo");
                ValidateUtil.validMaxLength(pack, "orderNo", 50);
                ValidateUtil.validNotNull(pack, "expressNo");
                ValidateUtil.validMaxLength(pack, "expressNo", 50);
                ValidateUtil.validNotNull(pack, "orderDate");
                ValidateUtil.validDateString(pack, "orderDate", "yyyy-MM-dd HH:mm:ss");
            }
        }
    }
    
    // 效验重量价格大于0
    public void positiveNumber(JSONObject json, String key) {
        if (isExistValue(json, key) && ValidateUtil.isBigDecimal(json.getString(key))) {
            BigDecimal bd = new BigDecimal(json.getString(key));
            if (bd.compareTo(new BigDecimal("0")) <= 0) {
                throw new ApiException(ErrorCode.FIELD_INPUT_ERROR, key);
            }
        }
    }
    
    @RequestMapping(value = "/getByPickupId")
    public String getByPickupId(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "pickupId");
            ISPOrderPickUpMessageCommand command = new ISPOrderPickUpMessageCommand();
            command.setCtx(CommandContext.getContext());
            ISPOrderPickUpMessage iSPOrderPickUpMessage =new ISPOrderPickUpMessage();
            iSPOrderPickUpMessage.setPickupId(json.getString("pickupId"));
            Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
            iSPOrderPickUpMessage.setSellerId(bpartner.getValue());
            command.setiSPOrderPickUpMessage(iSPOrderPickUpMessage);
            ISPOrderPickUpMessage message = iSPOrderPickUpMessageService.findByPickupId(command);
            responseMsg.setData(message);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        
        return SUCCESS;
    }
    
    /**
     * 揽收单轨迹查询接口
     */
    @RequestMapping(value = "/queryTrackingByPickupId")
    public String queryTrackingByPickupId(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            GetOriginalTracesCommand command = new GetOriginalTracesCommand();
            command.setCtx(CommandContext.getContext());
            command.setOrderNo(json.getString("orderNo"));
            List<OriginalTrace> trackingList = iSPOrderPickUpMessageService.queryTrackingByPickupId(command);
            responseMsg.setData(trackingList);
        }  else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        
        return SUCCESS;
    }

}
